<?php
/**
 * @package Freshine
 * @since Freshine 2.1.0
 */

/*-----------------------------------------------------------------------------------*/
/*	Add "Slide Options" submenu
/*-----------------------------------------------------------------------------------*/
add_action('admin_menu', 'f9_add_slide_admin_submenus');
function f9_add_slide_admin_submenus() {
	
	if ( current_theme_supports('f9_slide') ) {
		add_submenu_page('theme-options', 'Slide Options', 'Slide Options', 'manage_options', 'slide-options', 'f9_slide_options_admin');
	}
}

/*-----------------------------------------------------------------------------------*/
/*	Slide Options Values
/*-----------------------------------------------------------------------------------*/
$f9_slide_effect = array("fade", "fadeZoom", "blindX", "blindY", " blindZ", "cover", "curtainX", "curtainY", "growX", "growY", "scrollUp", "scrollDown", "scrollLeft", "scrollRight", "scrollHorz", "scrollVert", "shuffle", "slideX", "slideY", "toss", "turnUp", "turnDown", "turnLeft", "turnRight", "uncover", "wipe", "zoom");

$slide_options = array (
array(
"name" => "Choose an Effect",
"id" => $shortname."_slide_effect",
"std" => 0,
"options" => $f9_slide_effect,
"type" => "select"),

array(
"name" => "Speed",
"desc" => "The speed option defines the number of milliseconds it will take to transition from one slide to the next",
"id" => $shortname."_slide_speed",
"std" => '1000',
"type" => "text"),

array(
"name" => "Timeout",
"desc" => "The timeout option specifies how many milliseconds will elapse between the start of each transition",
"id" => $shortname."_slide_timeout",
"std" => '3600',
"type" => "text"),

array(
"name" => "Click Transitions",
"desc" => "Disable prev and next?",
"id" => $shortname."_trans_nextprev",
"type" => "checkbox",
"std" => "true"),

array(
"name" => "Slide Width",
"desc" => "px",
"id" => $shortname."_slide_width",
"std" => '940',
"type" => "text"),

array(
"name" => "Slide Height",
"desc" => "px",
"std" => '360',
"id" => $shortname."_slide_height",
"type" => "text"),

);

/*-----------------------------------------------------------------------------------*/
/*	Add a Theme Options Page
/*-----------------------------------------------------------------------------------*/
add_action('admin_menu', 'f9_add_slide_admin');
function f9_add_slide_admin() {
    global $themename, $shortname, $slide_options;
    if ( isset($_GET['page']) && $_GET['page'] == 'slide-options' ) {
        if ( isset($_REQUEST['action']) && 'save' == $_REQUEST['action'] ) {
                foreach ($slide_options as $value) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
                foreach ($slide_options as $value) {
                    if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
                header("Location: admin.php?page=slide-options&saved=true");
                die;
        } else if( isset($_REQUEST['action']) && 'reset' == $_REQUEST['action'] ) {
            foreach ($slide_options as $value) {
                delete_option( $value['id'] ); }
            	header("Location: admin.php?page=slide-options&reset=true");
            die;

        }
    }
}

/*-----------------------------------------------------------------------------------*/
/*	Slide Options Display
/*-----------------------------------------------------------------------------------*/
function f9_slide_options_admin() {
	global $themename, $shortname, $slide_options; ?>
<div class="wrap">
	<?php screen_icon('options-general'); ?>
	<h2>Slide Options</h2>
	<?php 
		if ( isset($_REQUEST['saved']) ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' Slide options saved.</strong></p></div>';
		if ( isset($_REQUEST['reset']) ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' Slide options reset.</strong></p></div>';
	?>
	<form method="post">
		<table class="form-table">
			<tbody>
			<?php
			foreach ($slide_options as $value) {
			switch ( $value['type'] ) {
				//Title
				case 'title': ?>
			</tbody>
		</table>
		<h3 class="title"><?php echo $value['name']; ?></h3>
		<table class="form-table">
			<tbody>
				<?php break;
				
				//Text field
				case 'text': ?>
				<tr valign="top">
					<th scope="row"><label for="posts_per_page"><?php echo $value['name']; ?></label></th>
					<td><input class="regular-text" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_option( $value['id'] ) != "") { echo stripslashes(get_option( $value['id'] )); } else { if(isset($value['std'])) echo stripslashes($value['std']); } ?>">
					<span class="description"><?php if(isset($value['desc'])) echo $value['desc']; ?></span>
					</td>
				</tr>
				<?php break;
		
				//Textarea
				case 'textarea': ?>
				<tr valign="top">
					<th scope="row"><label for="posts_per_page"><?php echo $value['name']; ?></label></th>
					<td>
					<textarea name="<?php echo $value['id']; ?>" class="large-text code" type="<?php echo $value['type']; ?>"><?php if ( get_option( $value['id'] ) != "") { echo stripslashes(get_option( $value['id'] )); } else { echo stripslashes($value['std']); } ?></textarea>
					<span class="description"><?php echo $value['desc']; ?></span>
					</td>
				</tr>
				<?php break;

				//Select
				case 'select': ?>
				<tr valign="top">
					<th scope="row"><label for="posts_per_page"><?php echo $value['name']; ?></label></th>
					<td>
						<select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
							<?php $i=0; foreach ($value['options'] as $option) { ?>
							<option value="<?php echo $i; ?>" <?php if ( get_option( $value['id'] ) == $i) { echo ' selected="selected"'; } ?>>
								<?php echo $option; ?>
							</option>
							<?php $i++; } ?>
						</select>
						<span class="description"><?php if(isset($value['desc'])) echo $value['desc']; ?></span>
					</td>
				</tr>
				<?php
				break;
	
				case "checkbox": ?>
				  <tr valign="top">
					<th scope="row"><label for="posts_per_page"><?php if(isset($value['name'])) echo $value['name']; ?></label></th>
					<td><?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
					  <input type="checkbox" name="<?php echo $value['id']; ?>" value="true" id="<?php echo $value['id']; ?>" <?php echo $checked; ?> />
					  <span><?php echo $value['desc']; ?></span></td>
				  </tr>
				  <?php
				break;
				}
			} ?>
			</tbody>
		</table>
		<p class="submit">
			<input name="save" type="submit" value="Save changes" class="button-primary" />
			<input type="hidden" name="action" value="save" />
		</p>
	</form>
	<form method="post">
		<p class="submit">
			<input name="reset" type="submit" value="Reset" />
			<input type="hidden" name="action" value="reset" />
		</p>
	</form>
</div>

<?php }
/*-----------------------------------------------------------------------------------*/
/*	Slide Display
/*-----------------------------------------------------------------------------------*/
function f9_slide( $slide_position = NULL, $before = '', $after = '' ) {
	if(get_option('f9_slide_width')) {
		$slide_width = get_option('f9_slide_width');
	} else {
		$slide_width = "940";
	}
	
	if(get_option('f9_slide_height')) {
		$slide_height = get_option('f9_slide_height');
	} else {
		$slide_height = "360";
	}
	// The Query
	$the_query = new WP_Query( 'post_type=slide&position='.$slide_position );
	if($the_query->have_posts()) :
	if($before) echo $before;
	?>
		<ul class="slideshow">
			<?php
			// The Loop
			while ( $the_query->have_posts() ) : $the_query->the_post();
				global $post;
				echo '<li>';
				if(get_post_meta($post->ID, 'slide-url', true)) echo '<a href="'.get_post_meta($post->ID, 'slide-url', true).'" title="'.get_the_title().'">';
				if(has_post_thumbnail()) the_post_thumbnail('',array( 'alt'	=> get_the_title(), 'title'	=> get_the_title() ));
				if(get_post_meta($post->ID, 'slide-url', true)) echo '</a>';
				if((get_post_meta($post->ID, 'hide-title', true)<>"true")&&(get_post_meta($post->ID, 'hide-content', true)<>"true")) {
					echo '<div class="slide-desc">';
					if(get_post_meta($post->ID, 'hide-title', true)<>"true") echo '<h2>'.get_the_title().'</h2>';
					if(get_post_meta($post->ID, 'hide-content', true)<>"true") echo get_the_content('');
					echo '</div>';
				}
				echo '</li>';
			endwhile;
			// Reset Post Data
			wp_reset_postdata();
			?>
		</ul>
		<?php if ((!(get_option('f9_trans_nextprev')))||(get_option('f9_trans_nextprev')==FALSE)) : ?>
		<a href="#" id="slide-nav-prev" class="slide-nav">Prev</a>
		<a href="#" id="slide-nav-next" class="slide-nav">Next</a>
		<?php endif; ?>
	<?php
	if($after) echo $after;
	endif;
}
function f9_slide_script() {
global $f9_slide_effect; ?>
<script src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js" type="text/javascript"></script>
<script type="text/javascript">

(function($) {
    jQuery('.slideshow').cycle({
		<?php
		//Slide Effect
		if(get_option('f9_slide_effect')) :
		echo "fx: '".$f9_slide_effect[get_option('f9_slide_effect')]."',";
		endif;
		
		//Slide Speed
		if(get_option('f9_slide_speed')) :
		echo "speed: ".get_option('f9_slide_speed').",";
		endif;
		
		//Slide Timeout
		if(get_option('f9_slide_timeout')) :
		echo "timeout: ".get_option('f9_slide_timeout').",";
		endif;
		
		//Slide Next/Prev
		if ((!(get_option('f9_trans_nextprev')))||(get_option('f9_trans_nextprev')==FALSE)) : ?>
		prev: '#slide-nav-prev',
        next: '#slide-nav-next',
		<?php endif; ?>
	});
})(jQuery);
</script>

<?php }

/*-----------------------------------------------------------------------------------*/
/*	Register Slide Post Type
/*-----------------------------------------------------------------------------------*/
function freshine_slide_post_types_init() {
	register_post_type( 'slide',
		array(
			'label' => __( 'Slide', 'freshine' ),
			'labels' => array(
				'name' => __( 'Slides', 'freshine' ),
				'singular_name' => __( 'Slide', 'freshine' ),
				'add_new' => __( 'Add New', 'freshine' ),
				'add_new_item' => __( 'Add New Slide', 'freshine' ),
				'edit' => __( 'Edit', 'freshine' ),
				'edit_item' => __( 'Edit Slide', 'freshine' ),
				'new_item' => __( 'New Slide', 'freshine' ),
				'view' => __( 'View Slides', 'freshine' ),
				'view_item' => __( 'View Slide', 'freshine' ),
				'search_items' => __( 'Search Slides', 'freshine' ),
				'not_found' => __( 'No slides found', 'freshine' ),
				'not_found_in_trash' => __( 'No slides found in trash', 'freshine' ),
				'parent' => __( 'Parent Slides', 'freshine' ),
			),
			'show_ui' => true,
			'public' => true,
			'has_archive' => true,
			'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
		)
	);
	
	register_taxonomy( 'position',
        array('slide'),
        array(
            'hierarchical' => true,
            'labels' => array(
                    'name' => __( 'Positions', 'freshine'),
                    'singular_name' => __( 'Position', 'freshine'),
                    'search_items' =>  __( 'Search Slide Position', 'freshine'),
                    'all_items' => __( 'All Slide Positions', 'freshine'),
                    'parent_item' => __( 'Parent Slide Position', 'freshine'),
                    'parent_item_colon' => __( 'Parent Slide Position:', 'freshine'),
                    'edit_item' => __( 'Edit Slide Position', 'freshine'),
                    'update_item' => __( 'Update Slide Position', 'freshine'),
                    'add_new_item' => __( 'Add New Slide Position', 'freshine'),
                    'new_item_name' => __( 'New Slide Position', 'freshine')
            ),
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => true,
        )
    );
}


if ( current_theme_supports('f9_slide') || current_theme_supports('freshine_slide') ) {
	add_action( 'init', 'freshine_slide_post_types_init', 0 );
	add_action( 'wp_footer' , 'f9_slide_script' );
}